package com.cqemme.activitiservice.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cqemme.activitiservice.utils.EmptyChecker;
import com.cqemme.activitiservice.feign.SchoolServiceFeign;
import com.cqemme.comm.po.Result;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.*;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author daibin
 * 测试控制器
 */
@Slf4j
@RestController
@RequestMapping("/ac")
@CrossOrigin
@ApiIgnore
public class Acti {
    @Resource
    private RuntimeService runtimeService;

    @Resource
    private TaskService taskService;

    @Resource
    private ProcessEngine processEngine;

    @Resource
    private IdentityService identityService;

    @Resource
    private RepositoryService repositoryService;

    @Resource
    private HistoryService historyService;

    @Resource
    private SchoolServiceFeign schoolServiceFeign;

    /**
     * 启动流程,进入第一个任务节点,完成第一个任务节点(完成请假单的填写)
     *
     * @return
     */
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public Object test() {
        String student = "{\"IMSI-id\":\"460045734304556\",\"Electricity\":\"4108\",\"distance\":5604,\"orgCode\":500107002,\"Sos-Sign\":\"1\",\"schoolId\":500107002,\"Nb-Strength\":\"23\",\"IMEI-id\":\"869075030145728\",\"Gps\":[{\"Address\":\"重庆市渝中区石油路街道金银湾215附2号楼\",\"Latitude\":\"29.5490140822686\",\"_id\":\"5e13ebad7e8f074658cbec34\",\"Longitude\":\"106.492492133174\"}],\"Student\":{\"studentAge\":7,\"updateDate\":\"2020-01-02 14:01:58\",\"imeiNo\":869075030145728,\"studentTel\":18956895689,\"studentNumber\":20190625,\"studentName\":\"赵思\",\"idCardNo\":\"532101199906252356\",\"headImage\":\"https://images.cqemme.com/images/png/default/file662299160141103104.png\",\"dataStatus\":1,\"id\":93,\"studentSex\":1,\"createDate\":\"2020-01-02 14:01:58\"},\"sosSite\":\"in\",\"Step-Count\":\"270\"}";
        Map<String, Object> stuMap = JSONObject.parseObject(student, Map.class);
        String studentId = ((Map) stuMap.get("Student")).get("id") + "";
        Result getTeacher=schoolServiceFeign.queryAllByEnumType(studentId);
        Object reData=getTeacher.getData();
        if(EmptyChecker.notEmpty(reData)){
            JSONArray teList=JSONArray.parseArray(reData.toString());
            String tel=teList.getJSONObject(0).get("tel")+"";
            System.out.println(tel);
        }

        return "var";
    }

    /**
     * 启动流程,进入第一个任务节点,完成第一个任务节点(完成请假单的填写)
     *
     * @return
     */
    @RequestMapping(value = "/start", method = RequestMethod.GET)
    public Object start() {
        //这里的请假流程是只能是张三请假，李四审批

        //请假流程key    aaa
        String proKey = "sos_alarm_daibin";

        //启动请假流程，这个时候进入第一个任务节点，张三就有任务了
        Map<String, Object> var = new HashMap<>();
        var.put("aa", "shenpiLis1");
        var.put("schoolId", "daibin");
        runtimeService.startProcessInstanceByKey(proKey,var);

        //查询张三的任务，根据办理人(张三)查询的任务列表
//        Task task=taskService.createTaskQuery().taskAssignee(var.get("people")+"").singleResult();

//        System.out.println("本次任务ID:"+task.getId());
//        System.out.println("本次流程实例ID:"+task.getProcessInstanceId());
//        System.out.println("本次执行的实例ID:"+task.getExecutionId());
//        System.out.println("流程定义ID:"+task.getProcessDefinitionId());
//        System.out.println("本次任务名称:"+task.getName());
//        System.out.println("本次任务办理人:"+task.getAssignee());

//        taskService.complete(task.getId());
        return var;
//            runtimeService.setVariable(task.getProcessInstanceId(),"money",100);
//            runtimeService.setVariable(task.getExecutionId(),"测试7777","嗯嗯");
//            taskService.setVariable("18","over","下一等级");
//        Map<String,Object> var = new HashMap<>();
//        var.put("money",10001);
//        taskService.complete(task.getId(),var);


//        for(Task task : taskList){
//            System.out.println("本次任务ID:"+task.getId());
//            System.out.println("本次流程实例ID:"+task.getProcessInstanceId());
//            System.out.println("本次执行的实例ID:"+task.getExecutionId());
//            System.out.println("流程定义ID:"+task.getProcessDefinitionId());
//            System.out.println("本次任务名称:"+task.getName());
//            System.out.println("本次任务办理人:"+task.getAssignee());
////            runtimeService.setVariable(task.getProcessInstanceId(),"测试3331","嗯嗯");
////            runtimeService.setVariable(task.getExecutionId(),"测试7777","嗯嗯");
////            taskService.setVariable("18","over","下一等级");
//            Map<String,Object> var = new HashMap<>();
//            var.put("over","结束");
//            taskService.complete(task.getId(),var);
//
//        }

    }

    /**
     * 完成审批
     *
     * @return
     */
    @RequestMapping(value = "/apply", method = RequestMethod.POST)
    public Object apply(String taskId) {
//        taskService.complete(taskId);
        List<ProcessInstance> list= runtimeService.createProcessInstanceQuery().list();
        for(ProcessInstance pro : list){
            runtimeService.deleteProcessInstance(pro.getProcessInstanceId(),"删除原因");
        }
        //查询李四的任务，根据办理人(李四)查询的任务列表
//        List<Task> taskList=taskService.createTaskQuery().list();
//        Task task=taskList.get(0);
//        Task task = taskService.createTaskQuery().taskAssignee("老大").singleResult();
////        List<IdentityLink> link = taskService.getIdentityLinksForTask(task.getId());
//        System.out.println("本次任务ID:" + task.getId());
//        System.out.println("本次流程实例ID:" + task.getProcessInstanceId());
//        System.out.println("本次执行的实例ID:" + task.getExecutionId());
//        System.out.println("流程定义ID:" + task.getProcessDefinitionId());
//        System.out.println("本次任务名称:" + task.getName());
//        System.out.println("本次任务办理人:" + task.getAssignee());
//        taskService.complete(taskId);
//        for(IdentityLink a : link){
//            System.out.println("------------");
//            System.out.println("办理人信息："+a.getUserId());
//            System.out.println("身份："+a.getType());
//        }


//        taskService.complete(taskId);
//        runtimeService.setVariable("18","over","下一等级");

//        for(Task task : taskList){
//            System.out.println("本次任务ID:"+task.getId());
//            //流程图没有分支的情况下，流程实例ID和执行实例ID是一样的
//            System.out.println("本次流程实例ID:"+task.getProcessInstanceId());
//            System.out.println("本次执行的实例ID:"+task.getExecutionId());
//            System.out.println("流程定义ID:"+task.getProcessDefinitionId());
//            System.out.println("本次任务名称:"+task.getName());
//            System.out.println("本次任务办理人:"+task.getAssignee());
////            taskService.claim(task.getId(),"A");
//            taskService.complete(task.getId());
//
//        }
        Map<String, Object> resultMap = new HashMap<>();
        return resultMap;
    }

}
